package item.inkcloud.resourceservice.service;

import item.inkcloud.resourceservice.entity.Resource;
import item.inkcloud.resourceservice.feignClient.UserServiceFeignClient;
import item.inkcloud.resourceservice.mapper.ResoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;


@Service
@Transactional
public class ResoService {

    @Autowired
    ResoMapper resoMapper;

    @Autowired
    UserServiceFeignClient userServiceFeignClient;

    public int publishResource(Resource resource,String userid) {

    String dedicator= userServiceFeignClient.getUsernameById(userid);

    resource.setDedicator(dedicator);

    try {
        resoMapper.publishResource(resource);
        return 1;
    }catch(Exception e){return 0;}
    }


    public HashMap getResourceList(){

        HashMap<String,Object> resultMap = new HashMap<>();

        try {
            List<Resource> list = resoMapper.findAll();
            resultMap.put("status",1);
            resultMap.put("data",list);
            return resultMap;
        }catch (Exception e){

            resultMap.put("status",0);
            return resultMap;
        }

    }

}

